home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalToolTipUI.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  134 lines

  1. /*
  2.  * @(#)MetalToolTipUI.java    1.8 98/04/08
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.BorderFactory;
  28. import com.sun.java.swing.border.Border;
  29. import com.sun.java.swing.plaf.*;
  30. import com.sun.java.swing.plaf.basic.BasicToolTipUI;
  31.  
  32.  
  33. /**
  34.  * A Metal L&F extension of BasicToolTipUI.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.8 04/08/98
  44.  * @author Steve Wilson
  45.  */
  46. public class MetalToolTipUI extends BasicToolTipUI {
  47.  
  48.     static MetalToolTipUI sharedInstance = new MetalToolTipUI();
  49.     Font smallFont;                         
  50.     static JToolTip tip;
  51.     public static final int padSpaceBetweenStrings = 12;
  52.  
  53.     public MetalToolTipUI() {
  54.         super();
  55.     }
  56.  
  57.     public static ComponentUI createUI(JComponent c) {
  58.         return sharedInstance;
  59.     }
  60.  
  61.     public void installUI(JComponent c) {
  62.         super.installUI(c);
  63.     tip = (JToolTip)c;
  64.     Font f = c.getFont();
  65.     smallFont = new Font( f.getName(), f.getStyle(), f.getSize() - 2 );
  66.     }
  67.  
  68.     public void paint(Graphics g, JComponent c) {
  69.  
  70.         FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(g.getFont());
  71.         Dimension size = c.getSize();
  72.  
  73.         g.setColor(c.getBackground());
  74.         g.fillRect(0, 0, size.width, size.height);
  75.  
  76.     g.setColor(c.getForeground());
  77.     String tipText = ((JToolTip)c).getTipText();
  78.     if (tipText == null) {
  79.         tipText = "";
  80.     }
  81.     String keyText = getAcceleratorString();
  82.         g.drawString(tipText, 3, 2 + metrics.getAscent());
  83.     if (! (keyText.equals(""))) {  // only draw control key if there is one
  84.         g.setFont(smallFont);
  85.         g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
  86.         g.drawString(keyText, 
  87.                  metrics.stringWidth(tipText) + 3 + padSpaceBetweenStrings, 
  88.                  2 + metrics.getAscent());
  89.     }
  90.     }
  91.  
  92.     public Dimension getPreferredSize(JComponent c) {
  93.         FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(c.getFont());
  94.  
  95.     String tipText = ((JToolTip)c).getTipText();
  96.     if (tipText == null) {
  97.         tipText = "";
  98.     }
  99.  
  100.     Dimension d = new Dimension(metrics.stringWidth(tipText) + 6, metrics.getHeight() + 4);
  101.  
  102.     String key = getAcceleratorString();
  103.     if (! (key.equals(""))) {
  104.             metrics = Toolkit.getDefaultToolkit().getFontMetrics(smallFont);    
  105.         d.width += metrics.stringWidth(key) + padSpaceBetweenStrings;
  106.     }
  107.         return d;
  108.     }
  109.    
  110.     public String getAcceleratorString() {
  111.         JComponent comp = tip.getComponent();
  112.     if (comp == null) {
  113.         return "";
  114.     }
  115.     KeyStroke[] keys =comp.getRegisteredKeyStrokes();
  116.     String controlKeyStr = "";
  117.  
  118.     for (int i = 0; i < keys.length; i++) {
  119.  
  120.       char c = (char)keys[i].getKeyCode();
  121.       int mod = keys[i].getModifiers();
  122.       if ( mod == InputEvent.CTRL_MASK ) {
  123.           controlKeyStr = "cntl+"+(char)keys[i].getKeyCode();
  124.           break;
  125.       } else if (mod == InputEvent.ALT_MASK) {
  126.           controlKeyStr = "alt+"+(char)keys[i].getKeyCode();
  127.           break;
  128.       } 
  129.     }
  130.     return controlKeyStr;
  131.     }
  132.  
  133. }
  134.